#include "sleefsview.h"
#include <QKeyEvent>
#include <QDir>
#include <sleefsmodel.h>
#include <QHeaderView>
#include <QModelIndex>
#include <QFocusEvent>
#include <QPainter>

#include <QDebug>

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

SleeFSView::SleeFSView(QWidget *parent) :
    QTableView(parent)
{
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Return:
    case Qt::Key_Enter:
        emit enter_pressed(currentIndex());
        return;
    case Qt::Key_Backspace:
        directory_up();
        return;
    case Qt::Key_Tab:
        emit tab_pressed(currentIndex());
        return;
    case Qt::Key_Insert:
        current_item_to_sel();
        return;
    }

    return QTableView::keyPressEvent(event);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::focusInEvent(QFocusEvent *event)
{
    QTableView::focusInEvent(event);
    emit got_focus();
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::focusOutEvent(QFocusEvent *event)
{
    QTableView::focusOutEvent(event);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::paintEvent(QPaintEvent *e)
{

    QTableView::paintEvent(e);
    QPainter painter(viewport());
    QRect rect = viewport()->rect();
    QBrush qb;
    if(m_is_active)
        painter.setPen(QPen(Qt::white, 4));
    else
    {
        painter.fillRect(rect, QColor(0,0,0,75));
        painter.setPen(QPen(Qt::darkGray, 4));
    }
    painter.drawRect(rect);

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::current_item_to_sel()
{
    QModelIndex index = currentIndex();
    if(!index.isValid()) return;
    SleeFSModel *p_model = dynamic_cast<SleeFSModel *>(model());
    p_model->or_to_select(index);
    QModelIndex new_index = p_model->index(index.row() + 1, 0, rootIndex());
    if(!new_index.isValid())  return;
    selectRow(new_index.row());
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::cd(const QDir &dir)
{
    SleeFSModel *p_model = dynamic_cast<SleeFSModel *>(model());
    setRootIndex(p_model->setRootPath(dir.absolutePath()));
    QItemSelectionModel *p_sel_model =  selectionModel();
    p_sel_model->clearSelection();
    p_model->clear_selection();
}


//--------------------------------------------------------------------------------------------------------------------------------------------------------------


void SleeFSView::directory_up()
{
    SleeFSModel *p_model = dynamic_cast<SleeFSModel *>(model());
    QDir dir(p_model->rootPath());
    QString back_path = dir.absolutePath();

//    qDebug()<<Q_FUNC_INFO<<"back_path: "<<back_path;
    if(!dir.cdUp()) return;
    cd(dir);
    QModelIndex back_index = p_model->index(back_path);
    if(!back_index.isValid()) return;
//    qDebug()<<Q_FUNC_INFO<<" index to select ...ok";
    selectRow(back_index.row());
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::resizeEvent(QResizeEvent *event)
{
    QTableView::resizeEvent(event);

    int tv_width = width();
    int ncols = horizontalHeader()->count();

    setColumnWidth(0, tv_width/2);

    tv_width -= columnWidth(0);
    ncols --;

    for(int i = 1; i< ncols; i++)
        setColumnWidth(i, tv_width / ncols);

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::set_active(bool state)
{
    m_is_active = state;
    if(state)
    {
        if(!currentIndex().isValid())
        {
            qDebug()<<" not valid index!";
        }
    }
    update();
    updateGeometry();
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

QStringList SleeFSView::selected_paths() const
{
    SleeFSModel *p_model = dynamic_cast<SleeFSModel *>(model());
    QStringList qstrl_ret = p_model ->selected_paths();

    if(qstrl_ret.isEmpty())
    {
        QModelIndexList ql_indices = selectionModel()->selection().indexes();

        foreach(QModelIndex index, ql_indices)
        {
            QString path = p_model->filePath(index);
            if(!qstrl_ret.contains(path))
                qstrl_ret.append(path);
        }
    }

    return qstrl_ret;
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

QString SleeFSView::root_path() const
{
    return dynamic_cast<SleeFSModel *>(model())->rootPath();
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

void SleeFSView::update_view()
{
    SleeFSModel *p_model = dynamic_cast<SleeFSModel *>(model());
    p_model->revert();
    QWidget::update();
    QWidget::updateGeometry();

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------

